from Products.KnowledgeEngine.KERL.Operators import *
from Products.KnowledgeEngine.KERL.KERLExceptions import *

class Token(KERLObject):
    
    _implements = KERLObject._implements + ("Token",)
    
    __allow_access_to_unprotected_subobjects__ = 1
        
    operators = {

    }

    def getOperators(self):
        """ Return this tokens operators """
        
        return self.operators
    
    @classmethod
    def TokenOperatorNames(cls):
        """ Returns this classes operators """
        
        ops = dict.fromkeys( [ operator.OperatorName() for operator in cls.operators.values() ] ).keys()
        ops.sort()
        return ops
    
    
    def getObject(self):
        return self.object
    
    def __init__(self, object, namespace):
        """ Token wraps this object """
        
        self.object    = object
        self.namespace = namespace        
        
    def findOperator(self, operator):
        """ Finds the relevant operator """
         
        try:
            return self.operators[ operator ]
        except KeyError:
            raise InvalidKerlOperator, \
                  "Cannot find operator '%s' for '%s'" % (operator, self.toEnglish())
    
    def toEnglish(self):
        """ Compiles this token into english """
        
        raise "NotImplemented", self.__class__.__name__ + "::toEnglish"
    
    def toSQL(self):
        """ Compiles this token into english """
        
        raise "NotImplemented", self.__class__.__name__ + "::toSQL"
    
    def toJavascript(self):
        """ Compiles this token into english """
        
        raise "NotImplemented", self.__class__.__name__ + "::toJavascript"
    
    def toKERL(self):
        """ Compiles this token into english """
        
        raise "NotImplemented", self.__class__.__name__ + "::toKERL"    
    
  
    def getAssessmentValue(self, assessment):
        """ Returns the value of this token for the given assessment """
        
        return self.getObject()
    
    
    # -----------------------------------------------------------------------------

    def equals(self, other, assessment):
        """ Overload the Equals operator for Components """
                
        myValue    = self.getAssessmentValue(assessment)
        otherValue = other.getAssessmentValue(assessment)

        return myValue == otherValue
        
    # -----------------------------------------------------------------------------
        
    def contains(self, other, assessment):            
        """ Overload the contains operator for Components """
        
        myValue    = self.getAssessmentValue(assessment)
        otherValue = other.getAssessmentValue(assessment)

        print "Other Value: " + `otherValue`
        print "My Value: " + `myValue`

        return otherValue in myValue    
    
    # -----------------------------------------------------------------------------
  
    @classmethod
    def registerOperator( cls, expression, operator ):
        """ Registers a KERL Operator against this token 
        
            e.g. registerOperator( '=', EqualsOperator )
        
            Will overwrite any existing operators registered with the given expression
        """
        
        cls.operators[ expression ] = operator
        